package com.hailiao.beidou.message.ui.page.home.view

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.d10ng.compose.ui.AppColor
import com.d10ng.compose.ui.AppText
import com.d10ng.compose.ui.base.CellRow
import com.d10ng.compose.ui.show.Badge
import com.d10ng.compose.ui.show.BadgeBox
import com.d10ng.datelib.isToday
import com.d10ng.datelib.isYesterday
import com.d10ng.datelib.toDateStr
import com.hailiao.beidou.message.R
import com.hailiao.beidou.message.data.db.constant.SendStatus
import com.hailiao.beidou.message.data.db.entity.ChatWithContactEntity
import com.hailiao.beidou.message.ui.view.ContactAvatar

/**
 * 聊天框
 * @Author d10ng
 * @Date 2023/11/15 20:38
 */
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun ChatCell(
    value: ChatWithContactEntity,
    longPress: Boolean = false,
    onClick: () -> Unit = {},
    onClickDelete: () -> Unit = {},
) {
    var expanded by remember { mutableStateOf(false) }
    val name = remember(value) {
        value.contact?.name?: value.chat.number
    }
    CellRow(
        modifier = Modifier
            .background(Color.White)
            .combinedClickable(
                onClick = onClick,
                onLongClick = {
                    if (longPress) expanded = true
                }
            ),
        divider = true,
        contentPaddingValues = PaddingValues(start = 15.dp, end = 19.dp, top = 8.dp, bottom = 8.dp)
    ) {
        // 头像
        BadgeBox(badge = { Badge(value.chat.unreadCount, color = AppColor.Main.primary) }) {
            ContactAvatar(name = name)
        }
        Column(
            modifier = Modifier
                .weight(1f)
                .padding(start = 12.dp)
        ) {
            Row(
                modifier = Modifier
                    .fillMaxWidth(),
                verticalAlignment = Alignment.CenterVertically
            ) {
                // 名称
                Text(
                    text = name,
                    style = AppText.Normal.Title.medium,
                    maxLines = 1,
                    overflow = TextOverflow.Ellipsis
                )
                Spacer(modifier = Modifier.weight(1f))
                // 时间
                val timeText = remember(value) {
                    val time = value.chat.time
                    when {
                        time.isToday() -> time.toDateStr("HH:mm")
                        time.isYesterday() -> "昨天"
                        else -> time.toDateStr("yyyy/MM/dd")
                    }
                }
                Text(
                    text = timeText,
                    style = AppText.Normal.Tips.mini
                )
            }
            Row(
                modifier = Modifier
                    .padding(top = 5.dp)
                    .fillMaxWidth(),
                verticalAlignment = Alignment.CenterVertically
            ) {
                // 发送状态
                val statusIconId = remember(value) {
                    if (value.chat.draftContent.isNotEmpty()) null else
                        when (value.chat.sendStatus) {
                            SendStatus.FAILED.value -> R.drawable.chat_send_fail
                            SendStatus.SENDING.value -> R.drawable.chat_sending
                            else -> null
                        }
                }
                if (statusIconId != null) {
                    Image(
                        painter = painterResource(id = statusIconId),
                        contentDescription = null,
                        modifier = Modifier.size(12.dp),
                        contentScale = ContentScale.Fit
                    )
                }
                // 草稿标记
                if (value.chat.draftContent.isNotEmpty()) {
                    Text(
                        text = "[草稿]",
                        style = AppText.Normal.Primary.mini,
                        color = AppColor.Main.primary
                    )
                }
                // 消息内容
                val content = remember(value) {
                    when {
                        value.chat.draftContent.isNotEmpty() -> value.chat.draftContent
                        value.chat.content.isEmpty() -> "（请输入内容进行发送）"
                        else -> value.chat.content
                    }
                }
                Text(
                    text = content,
                    style = AppText.Normal.Tips.mini,
                    maxLines = 1,
                    overflow = TextOverflow.Ellipsis
                )
            }
        }
    }

    Box {
        DropdownMenu(
            expanded = expanded,
            onDismissRequest = { expanded = !expanded }
        ) {
            DropdownMenuItem(
                onClick = {
                    onClickDelete()
                    expanded = false
                },
                text = {
                    Text("删除", style = AppText.Normal.Title.default)
                }
            )
        }
    }
}